<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('结算单列表')" />
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: layout-latest" />
    <style>

    </style>
</head>
<body>
    <div class="ui-layout-center">
        <div class="col-sm-12 search-collapse">
            <div class="col-sm-6 m-t-xs">
                <div class="form-group">
                    <label class="col-sm-4  control-label">客户姓名：</label>
                    <label class="col-sm-8 control-label">[[${statement.customerName}]]</label>
                </div>
            </div>
            <div class="col-sm-6 m-t-xs">
                <div class="form-group">
                    <label class="col-sm-4 control-label">联系方式：</label>
                    <label class="col-sm-8 control-label">[[${statement.customerPhone}]]</label>
                </div>
            </div>
            <div class="col-sm-6 m-t-xs">
                <div class="form-group">
                    <label class="col-sm-4 control-label">车牌号码：</label>
                    <label class="col-sm-8 control-label">[[${statement.licensePlate}]]</label>
                </div>
            </div>
            <div class="col-sm-6 m-t-xs">
                <div class="form-group">
                    <label class="col-sm-4 control-label">汽车类型：</label>
                    <label class="col-sm-8 control-label">[[${statement.carSeries}]]</label>
                </div>
            </div>
            <div class="col-sm-6 m-t-xs">
                <div class="form-group">
                    <label class="col-sm-4 control-label">服务类型：</label>
                    <label class="col-sm-8 control-label" th:text="${@dict.getLabel('si_service_catalog',statement.serviceType)}"></label>
                </div>
            </div>
            <div class="col-sm-6 m-t-xs">
                <div class="form-group">
                    <label class="col-sm-4 control-label">到店时间：</label>
                    <label class="col-sm-8 control-label" th:text="${#dates.format(statement.actualArrivalTime,'yyyy-MM-dd HH:mm:ss')}"></label>
                </div>
            </div>
            <div class="col-sm-6 m-t-xs">
                <div class="form-group">
                    <label class="col-sm-4 control-label">总消费金额：</label>
                    <label id="totalMoney" class="col-sm-8 control-label">[[${statement.totalAmount}]]</label>
                </div>
            </div>
            <div class="col-sm-6 m-t-xs" >
                <div class="form-group">
                    <label class="col-sm-4 control-label">实付价格：</label>
                    <label id="payMoney" class="col-sm-8 control-label">[[${statement.totalAmount-statement.discountAmount}]]</label>
                </div>
            </div>
            <div class="col-sm-6 m-t-xs">
                <div class="form-group">
                    <label class="col-sm-4 control-label">优惠价格：</label>
                    <input id="discounts" type="number" th:value="${statement.discountAmount}"  step="0.01" name="discountAmount">
                </div>
            </div>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success disabled" id="addDates" onclick="addDates()">
                    <i class="fa fa-plus"></i> 保存
                </a>
                <a class="btn btn-warning" id="payPal"  onclick="payPal()">
                    <i class="fa fa-paypal"></i> 支付
                </a>
            </div>
            <table id="cartTable"></table>
        </div>
    </div>
    <div class="ui-layout-east">
        <div class="col-sm-12 search-collapse">
            <form id="customer-form">
                <div class="select-list">
                    <!--服务类高级查询-->
                    <ul>
                        <li>
                            <input type="hidden" name="params[saleStatus]" value="1">
                            名&emsp;&emsp;称: <input type="text" placeholder="请输入服务名称" name="params[name]"/>
                        </li>
                        <li>
                            是否套餐:
                            <select name="params[carPackage]" th:with="type=${@dict.getType('bus_car_ackage')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            服务分类:
                            <select name="params[serviceCatalog]" th:with="type=${@dict.getType('bus_car_catalog')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>

                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search('customer-form','serveTable')">
                                <i class="fa fa-search"></i>&nbsp;搜索</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="serveTable"></table>
        </div>
    </div>
</body>


<script>
    let statementId = [[${statement.id}]];
    function addDates() {
        $("#addDates").addClass("disabled");
        let tableDatas = $("#cartTable").bootstrapTable('getData',false);
        let discounts = $("#discounts").val();
        let rows = $.map(tableDatas,function (ele) {
            let row = {
                statementId:statementId,
                itemId:ele.itemId,
                itemName:ele.itemName,
                itemPrice:ele.itemPrice,
                itemQuantity:ele.itemQuantity,
            };
            return row;
        });
        rows.push({
            statementId: statementId,
            itemPrice: discounts
        });
        var config = {
            url: "/business/statementItem/saveItems",
            type: 'post',
            dataType: 'json',
            contentType:"application/json",
            data: JSON.stringify(rows),
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $("#saveBtn").addClass("disabled");
                $("#payBtn").removeClass("disabled");
                $.operate.ajaxSuccess(result);

            }
        };
        $.ajax(config);
    }
    function payPal() {
        $.modal.confirm("确认用户已支付?", function() {
            console.log(statementId);
            $.operate.post("/business/statementItem/pay",{statementId:statementId},function (data) {
                //判断是否成功支付(web_status:封装好的消息状态码)
                if (data.code == 1){
                    // //跳转结算单明细详情页
                    // let url = "business/statementItem/itemDetail?statementId="+statementId;
                    //刷新选项卡
                    refreshItem();

                }else{
                    //提示失败消息
                    $.modal.alert(data.msg);
                }
            })
        });
    }
    function addBtnFun(id){
        let $serveTable = $("#serveTable");
        let row = $serveTable.bootstrapTable('getRowByUniqueId',id);
        let data = $("#cartTable").bootstrapTable('getRowByUniqueId',id);
        if (data){
            data.itemQuantity = data.itemQuantity+1;
            $("#cartTable").bootstrapTable("updateRow",data);
        }else {
            data = {
                itemId:id,//服务单项id
                statementId:statementId,//结算单项id
                itemName:row.name,
                itemPrice:row.originalPrice,
                itemQuantity:1
            };
            let bootstrapTable = $("#cartTable").bootstrapTable("append",data);
        }
        updateData();
    }
    function minusBtnFun(id){
        var $cartTable = $("#cartTable");
        var data = $cartTable.bootstrapTable("getRowByUniqueId",id);
        var itemQuantity = data.itemQuantity;
        if(itemQuantity == 1){
            $cartTable.bootstrapTable("removeByUniqueId",id);
        }else {
            data.itemQuantity = data.itemQuantity-1;
            $cartTable.bootstrapTable("updateRow",data);
        }
        updateData();
    }
    function updateData(){
        $("#addDates").removeClass("disabled");
        let totalMoney = $("#totalMoney");//总消费金额
        let payMoney = $("#payMoney");//支付金额
        let discounts = $("#discounts");//优惠价格
        let totalMoneyValue=0;
        let tableDatas = $("#cartTable").bootstrapTable('getData',false);
        $.each(tableDatas,function (index,ele) {
            totalMoneyValue += (ele.itemPrice * ele.itemQuantity);
        });
        totalMoney.html(totalMoneyValue.toFixed(2));
        let count = discounts.val();
        if (count > totalMoneyValue){
            payMoney.html(totalMoneyValue);
            discounts.val(0);
            // alert("优惠价格不能大于总价格!");
            return;
        }
        payMoney.html((totalMoneyValue - discounts.val()).toFixed(2));
    }
    $(function () {
        $("#discounts").change(function () {
            updateData();
        });

    });



    $('body').layout({
        east__size: '30%',
    });
    $(function () {
        let prefix = ctx + "business/statementItem";
        var options = {
            id:'serveTable',
            url: prefix + "/queryForServiceItem",   //查询的数据地址
            uniqueId: "id",
            columns: [
                {
                    field: 'id',
                    title: '业务id',
                    visible: false
                },
                {
                    field: 'name',
                    title: '服务项目名称',
                    sortable: true
                },
                {
                    field: 'originalPrice',
                    title: '服务项价格',
                    sortable: true,
                },
                {
                    field: 'info',
                    title: '备注',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value, 3, "open");
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        let actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" ' +
                            'onclick="addBtnFun(\'' + row.id + '\')"><i class="fa fa-plus"></i></a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });
    let newadd = $(function () {
        let prefix = ctx + "business/statementItem";
        var options = {
            id:'cartTable',
            pagination:false,//分页功能
            showRefresh:true,//刷新按钮
            url: prefix + "/query/"+ statementId,   //查询的数据地址
            uniqueId:"itemId",
            columns: [
                {
                    field: 'itemId',
                    title: '结算单id',
                    visible: false
                },
                {
                    field: 'itemName',
                    title: '服务项目名称',
                    sortable: true
                },
                {
                    field: 'itemPrice',
                    title: '服务项价格',
                    sortable: true,
                },
                {
                    field: 'itemQuantity',
                    title: '数量',
                    sortable: true,

                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        let actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" ' +
                            'onclick="addBtnFun(\'' + row.itemId + '\')"><i class="fa fa-plus"></i></a> ');
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" ' +
                            'onclick="minusBtnFun(\'' + row.itemId + '\')"><i class="fa fa-minus"></i></a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });
</script>

</html>